from django.shortcuts import render
from django.http import JsonResponse
from django.core import serializers
from .models import Portfolios


# Create your views here.


def get_portfolio_list(request):
    portfolio_list = Portfolios.objects.select_related("trade_account","strategy").all()
    result = []
    for porfolio in portfolio_list:
        info = {}
        info['portfolio_id'] = porfolio.pk
        info['portfolio_name'] = porfolio.portfolios_name
        info['portfolio_desc'] = porfolio.portfolios_desc
        info['cur_cash'] = porfolio.cur_cash
        info['init_cash'] = porfolio.init_cash
        info['strategy_parameter'] = porfolio.strategy_parameter
        info['broker_account'] = porfolio.trade_account.broker_account
        info['broker_pass'] = porfolio.trade_account.broker_pass
        info['user_id'] = porfolio.trade_account.user.pk
        info['user_name'] = porfolio.trade_account.user.username
        info['user_email'] = porfolio.trade_account.user.email
        info['broker_keyword'] = porfolio.trade_account.broker.broker_keyword
        info['broker_name'] = porfolio.trade_account.broker.broker_name

        result.append(info)
    return JsonResponse(result, safe=False)

